package com.panfeng.xcloud.component.ocr;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.afs.model.v20180112.AuthenticateSigRequest;
import com.aliyuncs.afs.model.v20180112.AuthenticateSigResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.panfeng.xcloud.component.ocr.config.SlidingVerificationPropConfig;
import com.panfeng.xcloud.component.ocr.model.SlidingVerificationReqDTO;
import com.panfeng.xcloud.component.ocr.model.SlidingVerificationRespDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 阿里云滑动验证服务
 *
 * @author xiaobo
 * @date 2018-02-07
 */
@Slf4j
@Component
public class SlidingVerificationMaker {

    @Autowired
    private SlidingVerificationPropConfig slidingVerificationPropConfig;

    /**
     * 开始进行滑动验证
     *
     * @param slidingVerificationReqDTO
     * @return
     * @throws ClientException
     */
    public SlidingVerificationRespDTO slidingVerification(SlidingVerificationReqDTO slidingVerificationReqDTO){
        String regionid = slidingVerificationPropConfig.getRegionid();
        String accessKeyId = slidingVerificationPropConfig.getAccessKeyId();
        String accessKeySecret = slidingVerificationPropConfig.getAccessKeySecret();
        String appKey = slidingVerificationPropConfig.getAppKey();
        String remoteIp = slidingVerificationPropConfig.getRemoteIp();
        IClientProfile profile = DefaultProfile.getProfile(regionid, accessKeyId, accessKeySecret);
        IAcsClient client = new DefaultAcsClient(profile);
        try {
            DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", "afs", "afs.aliyuncs.com");
        } catch (ClientException e) {
            log.info(">>> 初始化阿里云滑动验证服务异常：{}",e);
            e.printStackTrace();
        }
        AuthenticateSigRequest request = new AuthenticateSigRequest();
        // 会话ID。必填参数，从前端获取，不可更改。
        request.setSessionId(slidingVerificationReqDTO.getSessionId());
        // 签名串。必填参数，从前端获取，不可更改。
        request.setSig(slidingVerificationReqDTO.getSig());
        // 请求唯一标识。必填参数，从前端获取，不可更改。
        request.setToken(slidingVerificationReqDTO.getToken());
        // 场景标识。必填参数，从前端获取，不可更改。
        request.setScene(slidingVerificationReqDTO.getScene());
        // 应用类型标识。必填参数，后端填写。
        request.setAppKey(appKey);
        // 客户端IP。必填参数，后端填写。
        request.setRemoteIp(remoteIp);
        SlidingVerificationRespDTO slidingVerificationRespDTO = new SlidingVerificationRespDTO();
        try {
            // response的code枚举：100验签通过，900验签失败
            AuthenticateSigResponse response = client.getAcsResponse(request);
            slidingVerificationRespDTO.setCode(response.getCode());
            slidingVerificationRespDTO.setMsg(response.getMsg());
            slidingVerificationRespDTO.setDetail(response.getDetail());
            slidingVerificationRespDTO.setRequestId(response.getRequestId());
            slidingVerificationRespDTO.setRiskLevel(response.getRiskLevel());
        } catch (Exception e) {
            log.info(">>> 进行阿里云滑动验证服务异常：{}",e);
            e.printStackTrace();
        }
        return slidingVerificationRespDTO;
    }

}
